package com.insurance.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.common.enums.ApprovalStateEnum;
import com.insurance.common.enums.DeleteFlagEnum;
import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.Query;
import com.insurance.dao.AuthenticationEnterpriseDao;
import com.insurance.entity.AuthenticationEnterpriseEntity;
import com.insurance.service.AuthenticationEnterpriseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 企业业务接口实现类
 */
@Service("authenticationEnterpriseService")
public class AuthenticationEnterpriseServiceImpl extends ServiceImpl<AuthenticationEnterpriseDao, AuthenticationEnterpriseEntity> implements AuthenticationEnterpriseService {

    @Override
    public PageUtils queryPage(Map<String, Object> params, QueryWrapper<AuthenticationEnterpriseEntity> queryWrapper) {
        IPage<AuthenticationEnterpriseEntity> page = this.page(
                new Query<AuthenticationEnterpriseEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 获取查询条件
     * @return
     */
    public QueryWrapper<AuthenticationEnterpriseEntity> getQueryWrapper(){
        QueryWrapper<AuthenticationEnterpriseEntity> queryWrapper = new QueryWrapper<AuthenticationEnterpriseEntity>();
        queryWrapper.eq("delete_flag", DeleteFlagEnum.UN_DELETE.getCode());
        return queryWrapper;
    }

    @Override
    public QueryWrapper<AuthenticationEnterpriseEntity> getQueryWrapper(AuthenticationEnterpriseEntity authenticationEnterpriseEntity){
        QueryWrapper<AuthenticationEnterpriseEntity> queryWrapper = getQueryWrapper();
        if(authenticationEnterpriseEntity != null && !StringUtils.isBlank(authenticationEnterpriseEntity.getEnterpriseName())){
            queryWrapper.like("enterprise_name", authenticationEnterpriseEntity.getEnterpriseName());
        }
        return queryWrapper;
    }

    /**
     * 通过证件号校验企业是否注册过
     *
     * @param cardId 证件号
     * @return
     */
    @Override
    public AuthenticationEnterpriseEntity getByCardId(String cardId){
        QueryWrapper<AuthenticationEnterpriseEntity> queryWrapper = getQueryWrapper();
        queryWrapper.eq("certificate_no", cardId);
        return this.getOne(queryWrapper);
    }

    /**
     * 通过企业证件号查询企业
     * @param cardId
     * @param appId
     * @return
     */
    @Override
    public AuthenticationEnterpriseEntity getByCardIdAndAppId(String cardId, String appId){
        if(StringUtils.isBlank(cardId) || StringUtils.isBlank(appId)){
            return null;
        }
        QueryWrapper<AuthenticationEnterpriseEntity> queryWrapper = getQueryWrapper();
        queryWrapper.eq("certificate_no", cardId);
        queryWrapper.eq("app_id", appId);
        return this.getOne(queryWrapper);
    }


    public List<AuthenticationEnterpriseEntity> getListByCardId(String cardId){
        QueryWrapper<AuthenticationEnterpriseEntity> queryWrapper = getQueryWrapper();
        queryWrapper.eq("certificate_no", cardId);
        return this.list(queryWrapper);
    }

    /**
     * 通过证件号校验企业是否注册过
     *
     * @param cardId 证件号
     * @return 返回true表示数据库没有此企业, 校验通过, 反之不通过
     */
    @Override
    public boolean validateByCardId(String cardId){
        if(StringUtils.isBlank(cardId)){
            return false;
        }
        List<AuthenticationEnterpriseEntity> enterpriseEntityList = getListByCardId(cardId);
        return CollectionUtil.isEmpty(enterpriseEntityList);
    }

    /**
     * 通过主键集合查询
     *
     * @param idList 主键集合
     * @return
     */
    @Override
    public List<AuthenticationEnterpriseEntity> queryEnterpriseListByIds(List<String> idList){
        if(CollectionUtil.isEmpty(idList)){
            return new ArrayList<AuthenticationEnterpriseEntity>();
        }
        QueryWrapper<AuthenticationEnterpriseEntity> queryWrapper = getQueryWrapper();
        queryWrapper.in("id", idList);
        return this.list(queryWrapper);
    }

    /**
     * 获取lambda查询条件
     *
     * @return
     */
    public LambdaQueryWrapper<AuthenticationEnterpriseEntity> getLambdaQueryWrapper(){
        return getQueryWrapper().lambda();
    }

    /**
     * 查询企业集合
     *
     * @param authenticationEnterprise 查询条件
     * @return
     */
    @Override
    public List<AuthenticationEnterpriseEntity> queryEnterprise(AuthenticationEnterpriseEntity authenticationEnterprise){
        LambdaQueryWrapper<AuthenticationEnterpriseEntity> lambdaQueryWrapper = getLambdaQueryWrapper();
        if(authenticationEnterprise != null) {
            if (!StringUtils.isBlank(authenticationEnterprise.getId())) {
                lambdaQueryWrapper.eq(AuthenticationEnterpriseEntity::getId, authenticationEnterprise.getId());
            }
            if (!StringUtils.isBlank(authenticationEnterprise.getEnterpriseName())) {
                lambdaQueryWrapper.like(AuthenticationEnterpriseEntity::getEnterpriseName, authenticationEnterprise.getEnterpriseName());
            }
            if (!StringUtils.isBlank(authenticationEnterprise.getCertificateNo())) {
                lambdaQueryWrapper.like(AuthenticationEnterpriseEntity::getCertificateNo, authenticationEnterprise.getCertificateNo());
            }
            if (!StringUtils.isBlank(authenticationEnterprise.getLegalPerson())) {
                lambdaQueryWrapper.like(AuthenticationEnterpriseEntity::getLegalPerson, authenticationEnterprise.getLegalPerson());
            }
            if (!StringUtils.isBlank(authenticationEnterprise.getLegalCardId())) {
                lambdaQueryWrapper.like(AuthenticationEnterpriseEntity::getLegalCardId, authenticationEnterprise.getLegalCardId());
            }
            if (!StringUtils.isBlank(authenticationEnterprise.getPhoneNumber())) {
                lambdaQueryWrapper.like(AuthenticationEnterpriseEntity::getPhoneNumber, authenticationEnterprise.getPhoneNumber());
            }
        }
        lambdaQueryWrapper.eq(AuthenticationEnterpriseEntity::getState, ApprovalStateEnum.PASS.getCode());
        lambdaQueryWrapper.eq(AuthenticationEnterpriseEntity::getDeleteFlag, DeleteFlagEnum.UN_DELETE.getCode());
        return this.list(lambdaQueryWrapper);
    }

}